/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.security.nls;

import org.springframework.context.i18n.LocaleContextHolder;
import org.vacoor.nothing.common.misc.util.Locales;
import org.vacoor.nothing.common.util.StringUtils;
import org.vacoor.nothing.security.nls.support.GoogleTickleTranslator;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;

/**
 * @author vacoor
 */
public class NlsSupport {
    private static final String LANG_DIR = "nls";
    private static final String LANG_PACK_PREFIX = "lang_";

    public static String t(String text) {
        return translate(text, LocaleContextHolder.getLocale());
    }

    public static String t(String text, String loc) {
        return translate(text, Locales.parse(loc));
    }

    public static String translate(String text, Locale loc) {
        String value = null;
            // 查找字典
            Properties props = getLangPack(loc);
            value = props.getProperty(text);
            if (StringUtils.hasText(value)) {
                return value;
            }

            // 如果没有翻译, 写入
            value = doTranslate(text, Locale.CHINA, loc);
            value = null != value ? value : "";
            props.put(text, value);
            save(loc, props);
            System.out.println("translate " + text + " to " + value);
        return value;
    }

    protected static String doTranslate(String text, Locale from, Locale to) {
        // DriverManager.getConnection()
        Translator translator = GoogleTickleTranslator.INSTANCE;
        // Translator translator = BingTranslator.INSTANCE;
        // Translator translator = BaiduTranslator.INSTANCE;
        // Translator translator = YouDaoTranslator.INSTANCE;
        //Translator translator = YouDaoTickleTranslator.INSTANCE;
        return translator.translate(text, from, to);
    }

    public static void save(Locale loc, Properties props) {
        File lang = ensureLangePack(loc);
        try {
            props.store(new OutputStreamWriter(new FileOutputStream(lang), Charset.forName("UTF-8")), "update " + new Date());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Properties getLangPack(Locale loc) {
        Properties props = new Properties();
        File file = ensureLangePack(loc);
        if (file.exists()) {
            try {
                props.load(new FileReader(file));
            } catch (IOException e) {
            }
        }
        return props;
    }

    private static File ensureLangePack(Locale loc) {
        File file = new File("/Users/vacoor/IdeaProjects/nothing/nothing-security/src/main/resources");
        File nls = new File(file, LANG_DIR);
        if (!nls.exists()) {
            nls.mkdirs();
        }

        File lang = new File(nls, LANG_PACK_PREFIX + loc + ".properties");
        return lang;
    }

    public static Properties get(Locale loc) {
        return getLangPack(loc);
    }
}
